<template>
	<view style="height: 100%;">
		<view v-if="isLogin" class="content">
			<view>
				<input type="text" placeholder="请输入课程名称" class="edt-title" placeholder-class="txt-holder"
					@input="inputGetName" />
			</view>
			<view class="updata-pic-container" @tap="tapToChoose">
				<image src="../../../static/logo/ic_publish.png" mode="aspectFill" class="pic"></image>
				<text class="input-holder">{{picName}}</text>
			</view>

			<image v-if="showPicture" :src="picUrl" mode="aspectFill" class="pic-url" @tap="showPicture=false"></image>

			<view class="head-course">
				<textarea placeholder="请输入课程描述" class="course-ms" @input="inutGetMs"></textarea>
			</view>
		</view>
		<view class="content-switch">
			<text class="switch-Text">是否开放课程</text>
			<switch type="switch" style="transform:scale(0.6)" color="red" @change="sChecked"></switch>
		</view>
		<view class="button">

			<text class="text-button" @tap="tapCommit">创建课程</text>
		</view>
	</view>
</template>

<script>
	let app = getApp()
	let name = ''
	let desc = ''
	let isno = false
	export default {
		data() {
			return {
				isLogin: app.globalData.token.length > 0,
				picName: '请点击选择焦点图片',
				picUrl: '',
				showPicture: true,

				open: false
			}
		},
		methods: {
			sChecked(e) {
				console.log(e.detail.value);
				isno = e.detail.value

			},
			tapToChoose() {
				let url = this.$params.host + this.$params.action_upload
				uni.chooseImage({
					success: res => {
						let path = res.tempFilePaths[0]
						this.$request.postFile(url, 'file', path, {}, res => {
							let json = JSON.parse(res.data)
							this.picName = json.data
							this.picUrl = this.$params.host + this.picName
							this.showPicture = true
						})
					}
				})
			},
			inputGetName(e) {
				name = e.detail.value
			},
			inutGetMs(e) {
				desc = e.detail.value
			},
			isLegal() {
				if (!this.$utils.checkLength(name, 2, '标题')) {
					return false
				}
				if (!this.$utils.checkLength(desc, 6, '内容')) {
					return false
				}
				if (this.picName == '请点击选择焦点图片') {
					uni.showToast({
						title: '请选择图片',
						icon: 'none'
					});
					return false
				}
				return true
			},
			tapCommit() {
				if (!this.isLegal()) {
					return
				}
				uni.showModal({
					title: '创建课程',
					content: '确认创建课程？',
					cancelText: '否',
					confirmText: '是',
					success: res => {
						if (res.confirm) {
							this.postCourse()
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			postCourse() {
				let data = {
					desc,
					cover: this.picName,
					name,
					token: app.globalData.token,
					tid: app.globalData.uid,
					open: isno
				}
				console.log(data)
				let url = this.$params.host + this.$params.teacher_course_add
				this.$request.post(url, data, res => {
					console.log(url);
					console.log(res);
					if (!res.success) {
						uni.showToast({
							title: res.message,
							icon: 'none'
						});
						return
					}
				})
			},
		}
	}
</script>

<style>
	@import url("addCourse.css");
</style>
